//logs.js
const app = getApp();
const util = require('../../utils/util.js')

Page({
  data: {

    /**
     * AuthorityDialog显示提示
     */
    showAuthorityDialog: false,

    tabBar: null,
    hint: '尚未完成',
    logs: [],
    /** 
     * 页面配置 
     */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
    predictList: [],
    number1: 0,
    number2: 0,
    number3: 0,
  },


  /**
   * AuthorityDialog相关
   */
  showAuthorityDialog: function () {
    this.setData({
      showAuthorityDialog: true,
    })
  },
  hideAuthorityDialog: function () {
    this.setData({
      showAuthorityDialog: !this.data.showAuthorityDialog
    });
    wx.reLaunch({
      url: '../index/index',
    });
  },


  onLoad: function () {
    var that = this;

    var hasAuthority = app.checkAccessAuthority(1);
    //console.log(hasAuthority);
    if (!hasAuthority) {
      // 显示用户无权限的弹窗
      that.showAuthorityDialog();
    } else {
      app.editTabBar();
    }

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    /**
     * 获取用户的预测点评
     */
    if (app.globalData.userType != 1) {
      wx.request({
        url: app.globalData.server + '/predict/predict/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);
          that.setData({
            predictList: res.data.data,
          });

          if (that.data.predictList != null) {
            var num1 = 0;
            var num2 = 0;
            var num3 = 0;
            for (var i = 0; i < that.data.predictList.length; i++) {
              var item = that.data.predictList[i];
              if (item.status == "unpaid") {
                num1++;
              } else if (item.status == "unresult") {
                num2++;
              } else if (item.status == "settled") {
                num3++;
              }
            }
            that.setData({
              number1: num1,
              number2: num2,
              number3: num3,
            });
          }
        }
      })
    }
  },
  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;

    // 指向发起预测点评页面
    if (e.detail.current == 2) {
      var hasAuthority = app.checkAccessAuthority(1);
      //console.log(hasAuthority);
      if (!hasAuthority) {
        // 显示用户无权限的弹窗
        that.showAuthorityDialog();
      } else {
        app.editTabBar();
      }
    }

    that.setData({ currentTab: e.detail.current });

  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      // 指向发起预测点评页面
      if (e.target.dataset.current == 2) {
        var hasAuthority = app.checkAccessAuthority(1);
        //console.log(hasAuthority);
        if (!hasAuthority) {
          // 显示用户无权限的弹窗
          that.showAuthorityDialog();

        } else {
          app.editTabBar();
        }
      }

      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },


  onShareAppMessage: function () {
    return {
      title: '自定义转发标题',
      path: '/page/user?id=123'
    }
  }
})
